//
//  GYDisCardViewController.m
//  GoodLuck
//
//  Created by 郭为勇 on 2018/3/3.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYDisCardViewController.h"
#import "GYSearchViewController.h"
#import "GYBasePopoverViewController.h"
#import "PopoverAnimation.h"
#import "GYSystemMessageViewController.h"
#import "GYWebViewViewController.h"
@interface GYDisCardViewController ()

/** 动画代理 */
@property (nonatomic, strong) PopoverAnimation *animationDelegate;
@property (weak, nonatomic) IBOutlet UILabel *inviter_codeLable;
@property (weak, nonatomic) IBOutlet UIImageView *codeImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeImageViewBottomHeightCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeLableTopHeightCons;

@end

@implementation GYDisCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    [self setupUI];
}

- (void)setupUI {
    self.codeImageViewBottomHeightCons.constant = heightScale(160, iphone6_H);
    self.codeLableTopHeightCons.constant = heightScale(70, iphone6_H);
    self.navigationItem.title = @"分销名片";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(rightButClick) button:nil];
    self.inviter_codeLable.text = [NSString stringWithFormat:@"邀请码:%@",self.userInfo.inviter_code];
    [self.codeImageView sd_setImageWithURL:[NSURL URLWithString:self.userInfo.codeimg]];
    
    //给二维码添加长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGR:)];
    [self.codeImageView addGestureRecognizer:longPress];
    
}

- (void)longPressGR:(UILongPressGestureRecognizer *)longPress {
    if (longPress.state == UIGestureRecognizerStateBegan) {
        if (self.codeImageView.image != nil) {
            //识别图中二维码
            //1. 初始化扫描仪，设置设别类型和识别质量
            CIDetector*detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{ CIDetectorAccuracy : CIDetectorAccuracyHigh }];
            //2. 扫描获取的特征组
            NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:self.codeImageView.image.CGImage]];
            //3. 获取扫描结果
            CIQRCodeFeature *feature = [features objectAtIndex:0];
            
            GYWebViewViewController *webViewVC = [[GYWebViewViewController alloc] initWithNibName:@"GYWebViewViewController" bundle:nil];
            webViewVC.url = [NSURL URLWithString:feature.messageString];
            [self.navigationController pushViewController:webViewVC animated:YES];
            NSLog(@"长按识别二维码结果：%@",feature.messageString);

        }
    }
}

- (void)rightButClick {
    GYBasePopoverViewController *basePopoverVC = [[GYBasePopoverViewController alloc] initWithNibName:@"GYBasePopoverViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    basePopoverVC.popoverViewSelectIndexCallBack = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                weakSelf.tabBarController.selectedIndex = 0;
                [weakSelf.tabBarController.tabBar setHidden:NO];
            }
                break;
                
            case 1:
            {
                GYSearchViewController *searchVC = [[GYSearchViewController alloc] init];
                [self.navigationController pushViewController:searchVC animated:YES];
            }
                break;
                
            case 2:
            {
                GYSystemMessageViewController *systemMessageVC = [[GYSystemMessageViewController alloc] initWithNibName:@"GYSystemMessageViewController" bundle:nil];
                systemMessageVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:systemMessageVC animated:YES];
            }
                break;
                
            default:
                break;
        }
    };
    basePopoverVC.titles = @[@"首页",@"搜索",@"消息"];
    basePopoverVC.images = @[@"home_w",@"search_w",@"message_w"];
    
    self.animationDelegate = [[PopoverAnimation alloc] init];
    self.animationDelegate.popoverFrame = CGRectMake(ScreenWidth-widthScale(130, iphone6_W)-5, 50, widthScale(130, iphone6_W), basePopoverVC.titles.count*50);
    basePopoverVC.transitioningDelegate = (id)weakSelf.animationDelegate;
    
    basePopoverVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:basePopoverVC animated:YES completion:nil];

}

@end
